﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Entity;
using HanGu.Common;

namespace HanGu.CRM.Areas.Roles.Controllers
{
    public class DefaultController : AdminBaseController
    {
        //
        // GET: /Roles/Default/

        public ActionResult Index()
        {
            var roles = serviceRoles.GetAll(SearchParma);
            return View(roles);
        }

        //
        // GET: /Roles/Default/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Roles/Default/Create

        public ActionResult Create(int id = 0)
        {
            Role model = new Role()
            {
                Creator = CurentUser.UserName,
                CreateTime = DateTime.Now
            };
            if (id > 0)
            {
                model = serviceRoles.GetByID(id);
            }
            List<VRolePurview> roles = serviceRoles.GetVRolePurviewByID(id);
            ViewData["Roles"] = roles;
            ViewData["Modules"] = serviceModule.GetAll();
            return View(model);
        }

        //
        // POST: /Roles/Default/Create

        [HttpPost]
        public string Create(Role model)
        {
            try
            {
                var retult = false;
                model.CreateTime = model.CreateTime ?? DateTime.Now;
                model.Creator = string.IsNullOrEmpty(model.Creator) ? CurentUser.UserName : model.Creator;
                retult = serviceRoles.Add(model);
                if (retult)
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        [HttpPost]
        public string Delete(int id)
        {
            try
            {
                if (serviceRoles.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }

    }
}
